//
//  WmcCalendarIconTemplateView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/7.
//

import UIKit

/// 日历图标水印模板
class WmcCalendarIconTemplateView: WmcBaseWatermarkTemplateView {
    lazy var calendarIconImageView: UIImageView = {
        let imageView = UIImageView(image: "wm_calendar_title_icon".wmc_image)
        return imageView
    }()

    /// 月
    lazy var monthTimeLabel: WmcDateLabel = {
        let label = WmcDateLabel.YP.Create(font: 13.yp.uiFont(weight: .medium),
                                           color: UIColor.white,
                                           alignment: .center)
        label.leftEdge = 0
        label.format = .zh_MM
        setShadow(label)
        return label
    }()

    /// 天
    lazy var dayTimeLabel: WmcDateLabel = {
        let label = WmcDateLabel.YP.Create(font: 18.yp.uiFont(weight: .medium),
                                           color: UIColor.wmc_mainTheme,
                                           alignment: .center)
        label.leftEdge = 0
        label.format = .dd
//        setShadow(label, color: .wmc_mainTheme)
        return label
    }()

    /// 时间
    lazy var hourTimeLabel: WmcDateLabel = {
        let label = WmcDateLabel.YP.Create(font: 24.yp.uiFont(weight: .medium),
                                           color: UIColor.white,
                                           alignment: .left)
        label.leftEdge = 0
        label.format = .HH_mm
        setShadow(label)
        return label
    }()

    override func config() {
        super.config()
        defaultWidth = 225.wmc_autoHor

        addSubview(calendarIconImageView)

        addSubview(monthTimeLabel)
        addSubview(dayTimeLabel)

        addSubview(hourTimeLabel)

        timeLabel.format = .yyyy_MM_dd_week
        timeLabel.leftEdge = 0
        setShadow(timeLabel)
        addSubview(timeLabel)

        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 0
        addSubview(stackView)
    }

    override func configWatermarkModel(_ watermarkModel: WmcWatermarkDetailModel, styleMode: WmcBaseWatermarkTemplateView.StyleMode) {
        super.configWatermarkModel(watermarkModel, styleMode: styleMode)

        monthTimeLabel.styleMode = styleMode
        dayTimeLabel.styleMode = styleMode
        hourTimeLabel.styleMode = styleMode
        timeLabel.styleMode = styleMode
        if let staticDate = watermarkModel.staticData.staticDate, styleMode.isStatic {
            monthTimeLabel.updateDate(staticDate)
            dayTimeLabel.updateDate(staticDate)
            hourTimeLabel.updateDate(staticDate)
            timeLabel.updateDate(staticDate)
        }
        
        let alpha = calculateAlpha(watermarkModel.alpha, min: 0, max: 0.6)
        calendarIconImageView.alpha = alpha

        var drawViews: [UIView] = []
        // 获取标题最大字数
        let maxTitleCount = watermarkModel.fieldTitleMaxCount
        watermarkModel.fields.forEach { option in
            guard option.isOpen else { return }
            switch option.fieldType {
            case .time: // 时间
                break // 不做处理

            case .addr: // 地点
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .location)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .lat: // 经纬
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .longitudLatitude)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .weather: // 天气
                let item = createWeatherItem(option: option,
                                             styleMode: styleMode,
                                             staticWeater: watermarkModel.staticData.staticWeater,
                                             maxTitleCount: maxTitleCount,
                                             weatherFormat: .weatherTemperature)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .altitude: // 海拔
                let item = createAltitudeItem(option: option,
                                              styleMode: styleMode,
                                              staticLocation: watermarkModel.staticData.staticLocation,
                                              maxTitleCount: maxTitleCount,
                                              altitudeFormat: .meters)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .azimuth: // 方位角
                let item = createAzimuthItem(option: option,
                                             styleMode: styleMode,
                                             staticAzimuth: watermarkModel.staticData.staticAzimuth,
                                             maxTitleCount: maxTitleCount,
                                             azimuthFormat: .default)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .decibe: // 分贝
                let item = createDecibeItem(option: option,
                                            styleMode: styleMode,
                                            maxTitleCount: maxTitleCount,
                                            decibeFormat: .describeDB)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .speed: // 速度
                let item = createSpeedItem(option: option,
                                           styleMode: styleMode,
                                           staticLocation: watermarkModel.staticData.staticLocation,
                                           maxTitleCount: maxTitleCount,
                                           speedFormat: .km_h)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .remark,
                 .record,
                 .text,
                 .custom: // 备注、记工、输入
                let item = createDefaultTextItem(option: option,
                                                 maxTitleCount: maxTitleCount)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .brand: // 品牌图
                configOptionBrand(option)

            default: break
            }
        }

        drawViews.forEach { view in
            setShadow(view)
        }
        // 添加进堆栈视图
        stackView.yp.arrangedSubviews = drawViews
    }

    override func makeConstraints() {
        calendarIconImageView.snp.makeConstraints { make in
            make.top.left.equalTo(8)
            make.size.equalTo(76)
        }

        monthTimeLabel.snp.makeConstraints { make in
            make.left.right.equalTo(calendarIconImageView)
            make.bottom.equalTo(calendarIconImageView.snp.centerY)
            make.height.equalTo(17)
        }

        dayTimeLabel.snp.makeConstraints { make in
            make.left.right.equalTo(calendarIconImageView)
            make.top.equalTo(calendarIconImageView.snp.centerY)
            make.bottom.equalTo(calendarIconImageView).offset(-9)
        }

        hourTimeLabel.snp.makeConstraints { make in
            make.top.equalTo(calendarIconImageView).offset(11)
            make.left.equalTo(calendarIconImageView.snp.right).offset(4)
            make.height.equalTo(33)
            make.right.equalTo(-8)
        }

        timeLabel.snp.makeConstraints { make in
            make.top.equalTo(hourTimeLabel.snp.bottom)
            make.left.equalTo(hourTimeLabel)
            make.height.equalTo(20)
            make.right.equalTo(-8)
        }

        super.makeConstraints()

        stackView.snp.makeConstraints { make in
            make.top.equalTo(calendarIconImageView.snp.bottom)
            make.left.equalTo(calendarIconImageView).offset(-2)
            make.right.equalTo(-8)
            if isShowCertificationWatermark {
                make.bottom.equalTo(certificationView.snp.top).offset(-4)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }
    }
}
